<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Home\Service\ScoreService;
use Home\Service\SheetService;
use Home\Service\UserService;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

    public function __construct()
    {
        parent::__construct();
        $this->assign('nav', 'index');
    }

    //系统首页
    public function index()
    {
        //1.获取热门推荐
        $service = new ScoreService();
        $scoreList = $service->scoreHot(8);
        $this->assign('scoreList', $scoreList);
        //2.获取最佳排行
        $sheetService = new SheetService();
        $map = [
            'is_delete' => 0
        ];
        $order = 'fav_count desc, click_count desc';
        $sheetList = $sheetService->sheetRank($map,1, 10, $order);
        $this->assign('sheetList', $sheetList);

        //3.获取在线用户
        $userService = new UserService();
        $userList = $userService->getRandomUser();
        $this->assign('userList', $userList);
        $this->assign('subtitle', 'recommand');  //header导航active判断
        $this->display();

    }

    /**
     * 滚动加载最佳排行
     * @param $page
     * @param $limit
     */
    public function sheetRank($page, $limit)
    {
        $sheetService = new SheetService();
        $map = array(
            'is_delete' => 0
        );
        $order = 'fav_count desc, click_count desc';
        $sheetList = $sheetService->sheetRank($map, $page, $limit, $order);
        if (!$sheetList) {
            $result['code'] = 600;
            $result['msg']  = 'no data';
            $result['data'] = [];
            $this->ajaxReturn($result);
        }
        $result['code'] = 200;
        $result['msg']  = 'success';
        $result['data'] = $sheetList;
        $this->ajaxReturn($result);
    }


    /**
     * 获取首页谱单列表分页
     */
    public function score()
    {
        $param = I('param.');
        $page = is_numeric($param['p']) ? $param['p'] : 1;
        //获取所有谱单
        $service = new ScoreService();
        $list = $service->getScoreList($page, 12, $count);
        $this->assign('score_list', $list);

        //分页
        $Page       = new \Think\Page($count,12);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        $this->assign('update_time', date('Y.m.d', time()));
        $this->assign('subtitle', 'score');  //header导航active判断
        $this->display('Index/score');
    }

    /**
     * 获取首页曲谱列表分页
     */
    public function sheet()
    {
        $param = I('param.');
        $page = is_numeric($param['p']) ? $param['p'] : 1;
        //获取所有曲谱
        $service = new SheetService();
        $map = array(
            'is_delete' => 0
        );
        $order = 'click_count desc';
        $list = $service->sheetRank($map, $page, 20, $order, $count);
        $this->assign('sheetList', $list);
        //分页
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        $this->assign('update_time', date('Y.m.d', time()));
        $this->assign('subtitle', 'sheet');  //header导航active判断
        $this->display('Index/sheet');
    }

}